<template>
    <div class="special-offers">
        <!-- 顶部导航栏 -->
        <header class="header">
            <i class="iconfont icon-xianshiyouhui"></i>
            <h1>商城特惠专区</h1>
        </header>

        <!-- 筛选栏 -->
        <div class="filter-bar">
            <div class="filter-item">
                <label>分类：</label>
                <select v-model="selectedCategory" @change="filterProducts">
                    <option value="">全部</option>
                    <option v-for="category in categories" :key="category" :value="category">
                        {{ category }}
                    </option>
                </select>
            </div>

            <div class="filter-item">
                <label>价格：</label>
                <select v-model="selectedPriceRange" @change="filterProducts">
                    <option value="">全部</option>
                    <option value="low">0-100</option>
                    <option value="medium">101-500</option>
                    <option value="high">500以上</option>
                </select>
            </div>
        </div>

        <!-- 商品展示网格 -->
        <div class="product-grid">
            <div class="product-item" v-for="product in filteredProducts" :key="product.id">
                <img :src="product.image" :alt="product.name" class="product-image" />
                <h3 class="product-title">{{ product.name }}</h3>
                <p class="product-price">￥{{ product.price }}</p>
                <button class="buy-button" @click="addToCart(product)">立即抢购</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useSpecialRecommendStore } from '../../../stores/recommend/specialrecommend'

const categories = ref(['电子产品', '服装', '食品', '家电', '家居', '户外', '运动']); // 商品分类

// 实例化推荐商品 store
const specialRecommendStore = useSpecialRecommendStore();
// 筛选条件
const selectedCategory = ref('');
const selectedPriceRange = ref('');

// 筛选后的商品
const filteredProducts = computed(() => {
    return specialRecommendStore.products.filter((product) => {
        // 分类筛选
        const categoryMatch = selectedCategory.value
            ? product.category === selectedCategory.value
            : true;

        // 价格筛选
        let priceMatch = true;
        if (selectedPriceRange.value === 'low') priceMatch = product.price <= 100;
        else if (selectedPriceRange.value === 'medium') priceMatch = product.price > 100 && product.price <= 500;
        else if (selectedPriceRange.value === 'high') priceMatch = product.price > 500;

        return categoryMatch && priceMatch;
    });
});

// 模拟添加到购物车
const addToCart = (product) => {
    alert(`已将商品【${product.name}】添加到购物车！`);
};

// 筛选商品
const filterProducts = () => {
    // 无需其他逻辑，使用 computed 自动更新
};
</script>

<style scoped lang="scss">
/* 页面整体样式 */
.special-offers {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

/* 顶部导航栏 */
.header {
    margin-bottom: 20px;
    font-size: 24px;
    font-weight: bold;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    i{
        font-size: 50px;
        color: #27ba9b;
    }
    h1 {
        font-size: 2rem;
        font-weight: bold;
        font-family: '宋体';
    }
}

/* 筛选栏样式 */
.filter-bar {
    display: flex;
    justify-content: space-evenly;
    margin-bottom: 20px;
}

.filter-item {
    label {
        font-size: 20px;
        margin-right: 10px;
        font-weight: bold;
    }

    select {
        padding: 8px;
        border-radius: 4px;
        background-color: #f9f9f9;
        cursor: pointer;
        transition: border-color 0.3s;

        option {
            padding: 10px;

            &:hover {
                background-color: #ececec;
            }
        }
    }
}

/* 商品展示网格 */
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
}

.product-item {
    border: 1px solid #eaeaea;
    padding: 15px;
    text-align: center;
    border-radius: 8px;
    background-color: #fff;
    transition: box-shadow 0.3s;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
}

.product-item:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.product-image {
    width: 100%;
    height: auto;
    margin-bottom: 10px;
}

.product-title {
    font-size: 16px;
    margin-bottom: 10px;
}

.product-price {
    color: #e74c3c;
    font-size: 18px;
    margin-bottom: 10px;
}

.buy-button {
    background-color: #27ba9b;
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.buy-button:hover {
    background-color: #e74c3c;
}
</style>